package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Address;
import com.rain.shoppingscienceproj.ui.activity.address.AddressActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2017/12/14 17:23
 */

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.MyViewHolder> {

    private Context mContext;
    public LongItemListener longItemListener;
    public OnItemClickListener onItemClickListener;
    private int type = 0;
    private List<Address> list;


    public AddressAdapter(Context mContext, int type) {
        this.mContext = mContext;
        this.type = type;
    }

    public void setList(List<Address> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setLongItemListener(LongItemListener longItemListener) {
        this.longItemListener = longItemListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Address addressData = list.get(position);
        if (addressData.getStatus().equals("2")) {
            holder.iv_check.setVisibility(View.VISIBLE);
        } else {
            holder.iv_check.setVisibility(View.INVISIBLE);
        }
        holder.tv_name.setText(addressData.getAddressName());
        holder.tv_phone.setText(addressData.getAddressPhone());
        if (!addressData.getAddress().isEmpty()) {
            String address = "";
            String[] strs = addressData.getAddress().split("-");
            for (int i = 0; i < strs.length; i++) {
                address += strs[i];
            }
            holder.tv_address.setText(address + addressData.getAddressDetils());
        }
        holder.iv_up_address.setOnClickListener(v -> startAct(addressData));
        holder.ll_address_del.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                longItemListener.LongItemLister(addressData, position);
                return false;
            }
        });
        holder.ll_address_del.setOnClickListener(v -> onItemClickListener.OnItemClickListener(addressData));
        if (type == 1)
            holder.iv_up_address.setVisibility(View.GONE);
        else holder.iv_up_address.setVisibility(View.VISIBLE);
    }

    private void startAct(Address addressData) {
        Intent intent = new Intent(mContext, AddressActivity.class);
        intent.putExtra("address", addressData);
        mContext.startActivity(intent);
    }


    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        else
            return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_check)
        ImageView iv_check;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_phone)
        TextView tv_phone;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.iv_up_address)
        ImageView iv_up_address;
        @BindView(R.id.ll_address_del)
        LinearLayout ll_address_del;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface LongItemListener {
        void LongItemLister(Address addressData, int position);
    }

    public interface OnItemClickListener {
        void OnItemClickListener(Address addressData);
    }
}
